﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	#endregion

	public partial class FrmChamDiemSinhVien : Office2007Form
	{
		public EventHandler Handler { get; set; }
        public int MaCTDK { get; set; }
        private DiemHocTap DiemHT = null;

        public FrmChamDiemSinhVien(int mactdk)
		{
			InitializeComponent();
            this.MaCTDK = mactdk;

            var lstDiemht = DiemHocTapRepository.SelectByMaCTDK(this.MaCTDK);
            if (lstDiemht.Count > 0)
            {
                DiemHT = lstDiemht[0];
                txtDiemQuaTrinh.Text = DiemHT.DiemQuaTrinh.ToString();
                txtDiemThi.Text = DiemHT.DiemThi.ToString();
                txtDiemTongKet.Text = DiemHT.DiemTongKet.ToString();
            }
		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
            if (string.Empty.Equals(txtDiemTongKet.Text))
            {
                MessageBoxEx.Show("Điểm tổng kết chưa được cập nhật", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (!string.Empty.Equals(txtDiemTongKet.Text))
            {
                if (double.Parse(txtDiemTongKet.Text) >= 5 && !string.Empty.Equals(txtDiemThiLan2.Text))
                {
                    MessageBoxEx.Show("Điểm thi lần 2 chỉ được cập nhật nếu điểm tổng kết < 5", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            DiemHocTap diemht = null;
            var result = false;
            if (DiemHT != null)
            {
                diemht = DiemHT;
                diemht.DiemQuaTrinh = double.Parse(txtDiemQuaTrinh.Text);
                diemht.DiemThi = double.Parse(txtDiemThi.Text);
                diemht.DiemTongKet = double.Parse(txtDiemTongKet.Text);
                diemht.DiemThiLan2 = double.Parse(txtDiemThiLan2.Text);
                result = DiemHocTapRepository.Save();
            }
            else
            {
                diemht = new DiemHocTap
                {
                    MaCTDK = this.MaCTDK,
                    DiemQuaTrinh = double.Parse(txtDiemQuaTrinh.Text),
                    DiemThi = double.Parse(txtDiemThi.Text),
                    DiemTongKet = double.Parse(txtDiemTongKet.Text),
                    DiemThiLan2 = double.Parse(txtDiemThiLan2.Text)
                };
                result = DiemHocTapRepository.Insert(diemht);
            }
            if (result)
            {
                TransferDataInfo(this, new TransferSimpleDataEvent(txtDiemQuaTrinh.Text + "#" + txtDiemThi.Text + "#" + txtDiemTongKet.Text + "#" + txtDiemThiLan2.Text));
            }
            else
            {
                MessageBoxEx.Show("Nhập điểm không thành công", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

        private void txtDiemQuaTrinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow type number
            if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void txtDiemThi_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow type number
            if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void txtDiemQuaTrinh_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtDiemThi.Text != string.Empty && txtDiemQuaTrinh.Text != string.Empty)
            {
                var diemtongket = Math.Round((double.Parse(txtDiemThi.Text) * 2 + double.Parse(txtDiemQuaTrinh.Text)) / 3, 1);
                txtDiemTongKet.Text = diemtongket.ToString();
            }
            else
            {
                txtDiemTongKet.Text = string.Empty;
            }
        }

        private void txtDiemThi_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtDiemThi.Text != string.Empty && txtDiemQuaTrinh.Text != string.Empty)
            {
                var diemtongket = Math.Round((double.Parse(txtDiemThi.Text) * 2 + double.Parse(txtDiemQuaTrinh.Text)) / 3, 1);
                txtDiemTongKet.Text = diemtongket.ToString();
            }
            else
            {
                txtDiemTongKet.Text = string.Empty;
            }
        }

        private void txtDiemThiLan2_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow type number
            if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }
	}

}
